import {
	request
} from './request.js';

import store from '../store/index.js'
import {mapState,mapMutations} from 'vuex';
// import {mapState,mapMutations} from 'vuex';
export async function handleRequest(e) {

	function formatterDateTime() {
		let date = new Date();
		let month = date.getMonth() + 1;
		return date.getFullYear() +
			"" // "年"
			+
			(month >= 10 ? month : "0" + month) +
			"" // "月"
			+
			(date.getDate() < 10 ? "0" + date.getDate() : date
				.getDate()) +
			"" +
			(date.getHours() < 10 ? "0" + date.getHours() : date
				.getHours()) +
			"" +
			(date.getMinutes() < 10 ? "0" + date.getMinutes() : date
				.getMinutes()) +
			"" +
			(date.getSeconds() < 10 ? "0" + date.getSeconds() : date
				.getSeconds());
	}

	var curPage = getCurrentPages()[getCurrentPages().length - 1].route.split("/")[1];

	// console.log(curPage);

	// _self.book.isbn = e.trim();

	// /*如果输入框为空*/
	

	/*重置book_index*/


	// _self.book_index = null;

	/*
	 * 如果是第一次查找该书，发起请求，并存入缓存
	 * 如果不是第一次查找该书，从books中取出
	 * */
	let saleBooks = uni.getStorageSync("cart") || [];
	let books = uni.getStorageSync("books") || [];
	let rentCart = uni.getStorageSync('rentCart') || [];
	let buyCart = uni.getStorageSync('buyCart') || [];
	if (books.length === 0) {
		/*如果books为空 */

		await request({
			url: "https://route.showapi.com/2218-1",
			method: "GET",
			dataType: "json",
			data: {
				"showapi_timestamp": formatterDateTime(),
				"showapi_appid": '214342', //这里需要改成自己的appid
				"showapi_sign": '7f0a4bbccc3949afa6ec8a46f51f4e2e', //这里需要改成自己的应用的密钥secret
				"isbn": e
			}
		}).then(value => {

			if (typeof(value) == "string") {

				uni.showModal({
					title: '提示',
					content: value,
					mask: true,
					showCancel: false
				});

				console.log(value);
				return;
			}

			// _self.book = value;



			books.push(value);

			// _self.books = books;
			uni.setStorageSync("books", books);

			/*跳转到书本详情页面 并传递book_index*/

			// _self.book_index = 0;

			console.log("跳转1");

			if (curPage == "index") {
				if(store.state.buyOffLine){
					store.state.buyOffLine = false;
					let book = {
						checked: true,
						img: books[0].bookImg,
						bookName: books[0].bookName,
						author: books[0].author,
						price: books[0].price,
						discount: 2.0,
						booksElement: 0,
						isbn:books[0].isbn,
						bookNum:1
					};
					buyCart.unshift(book);
					uni.setStorageSync("buyCart", buyCart);
					
					uni.navigateTo({
						url: '/pages/buyRentCart/buyRentCart?tabBar=0'
					});
				}else if(store.state.rentOffline){
					store.state.rentOffLine = false;
					let book = {
						checked: true,
						img: books[0].bookImg,
						bookName: books[0].bookName,
						author: books[0].author,
						price: books[0].price,
						discount: 2.0,
						booksElement: 0,
						isbn:books[0].isbn,
						bookNum:1
					};
					rentCart.unshift(book);
					uni.setStorageSync("rentCart", rentCart);
					uni.navigateTo({
						url: '/pages/buyRentCart/buyRentCart?tabBar=1'
					});
				}else{
					
					uni.navigateTo({
						url: '/pages/book_details/book_details?book_index=0'
					});
				}
				
			} else {
				let book = {
					checked: true,
					img: books[0].bookImg,
					bookName: books[0].bookName,
					author: books[0].author,
					price: books[0].price,
					discount: 2.0,
					booksElement: 0,
					isbn:books[0].isbn,
					bookNum:1
				};
				saleBooks.unshift(book);
				uni.setStorageSync("cart", saleBooks);

				if (curPage == 'sale') {
					uni.navigateTo({
						url: "/pages/cart/cart",
					});
				}

			}


		}).catch(reason => {


			uni.showModal({
				title: '错误',
				content: reason,
				mask: true,
				showCancel: false
			});

			console.log(reason);
		});



	} else {
		/*如果不是第一次搜索*/

		/*如果以往搜索过该书 则记录book_index*/
		for (var index in books) {
			if (books[index].isbn == e) {

				console.log("跳转2");
				
				if(store.state.rentOffLine){
						store.state.rentOffLine = false;
						if(rentCart.length==0){
							let book = {
								checked: true,
								img: books[index].bookImg,
								bookName: books[index].bookName,
								author: books[index].author,
								price: books[index].price,
								discount: 2.0,
								booksElement: index,
								isbn:books[index].isbn,
								bookNum:1
								// choosed:true,
							};
							
							rentCart.unshift(book);
							
							uni.setStorageSync("rentCart", rentCart);
						}else{
							for (var element in rentCart) {
								if (rentCart[element].img == books[index].bookImg) {
									/* 如果cart缓存里有该书，则退出循环*/
									console.log(1);
									uni.showToast({
							
										title: '已存在记录',
										mask: true
									})
									
									break;
								}
							
								if (element == rentCart.length - 1) {
									// 如果遍历了都不是该书则添加
							
									let book = {
										checked: true,
										img: books[index].bookImg,
										bookName: books[index].bookName,
										author: books[index].author,
										price: books[index].price,
										discount: 2.0,
										booksElement: index,
										isbn:books[index].isbn,
										bookNum:1
										// choosed:true,
									};
							
									rentCart.unshift(book);
							
									uni.setStorageSync("rentCart", rentCart);
								}
							}
						}
						
					
					uni.navigateTo({
						url: '/pages/buyRentCart/buyRentCart?tabBar=1'
					});
				}else if(store.state.buyOffLine){
						store.state.buyOffLine = false;
						if(buyCart.length==0){
							let book = {
								checked: true,
								img: books[index].bookImg,
								bookName: books[index].bookName,
								author: books[index].author,
								price: books[index].price,
								discount: 2.0,
								booksElement: index,
								isbn:books[index].isbn,
								bookNum:1
								// choosed:true,
							};
							
							buyCart.unshift(book);
							
							uni.setStorageSync("buyCart", buyCart);
						}else{
							for (var element in buyCart) {
								if (buyCart[element].img == books[index].bookImg) {
									/* 如果cart缓存里有该书，则退出循环*/
									console.log(2);
									uni.showToast({
							
										title: '已存在记录',
										mask: true
									})
									
									break;
								}
							
								if (element == buyCart.length - 1) {
									// 如果遍历了都不是该书则添加
							
									let book = {
										checked: true,
										img: books[index].bookImg,
										bookName: books[index].bookName,
										author: books[index].author,
										price: books[index].price,
										discount: 2.0,
										booksElement: index,
										isbn:books[index].isbn,
										bookNum:1
										// choosed:true,
									};
							
									buyCart.unshift(book);
							
									uni.setStorageSync("buyCart", buyCart);
								}
							}
						}
						
				
					
					uni.navigateTo({
						url: '/pages/buyRentCart/buyRentCart?tabBar=0'
					});
				}
				
				if (curPage == "index") {
						
						uni.navigateTo({
							url: '/pages/book_details/book_details?book_index=' + index
						});
					
					
				} else {
					if(saleBooks.length==0){
						let book = {
							checked: true,
							img: books[index].bookImg,
							bookName: books[index].bookName,
							author: books[index].author,
							price: books[index].price,
							discount: 2.0,
							booksElement: index,
							isbn:books[index].isbn,
							bookNum:1
							// choosed:true,
						};
						
						saleBooks.unshift(book);
						
						uni.setStorageSync("cart", saleBooks);
					}else{
						for (var element in saleBooks) {
							if (saleBooks[element].img == books[index].bookImg) {
								/* 如果cart缓存里有该书，则退出循环*/
								console.log(3);
								uni.showToast({
						
									title: '已存在记录',
									mask: true
								})
								
								break;
							}
						
							if (element == saleBooks.length - 1) {
								// 如果遍历了都不是该书则添加
						
								let book = {
									checked: true,
									img: books[index].bookImg,
									bookName: books[index].bookName,
									author: books[index].author,
									price: books[index].price,
									discount: 2.0,
									booksElement: index,
									isbn:books[index].isbn,
									bookNum:1
									// choosed:true,
								};
						
								saleBooks.unshift(book);
						
								uni.setStorageSync("cart", saleBooks);
							}
						}
					}
					
					
					

					if (curPage == 'sale') {
						uni.navigateTo({
							url: "/pages/cart/cart"

						});
					}





				}



				return;
			}
		}

		/*如果没有搜索过该书 则发起请求*/
		await request({
			url: "https://route.showapi.com/2218-1",
			method: "GET",
			dataType: "json",
			data: {
				"showapi_timestamp": formatterDateTime(),
				"showapi_appid": '214342', //这里需要改成自己的appid
				"showapi_sign": '7f0a4bbccc3949afa6ec8a46f51f4e2e', //这里需要改成自己的应用的密钥secret
				"isbn": e
			}
		}).then(value => {

			if (typeof(value) == "string") {

				uni.showModal({
					title: '提示',
					content: value,
					mask: true,
					showCancel: false
				});

				console.log(value);
				return;
			}

			books.push(value);

			// _self.books = books;
			uni.setStorageSync("books", books);


			// _self.book_index = _self.books.length - 1;

			console.log("跳转3");

			// if(curPage !== "cart"){
			// 	uni.redirectTo({
			// 	    url:pageUrl+"?book_index=" + _self.book_index
			// 	});
			// }
			var book_index = books.length - 1;

			if (curPage == "index") {
				if	(store.state.buyOffLine){
						store.state.buyOffLine = false;
						let book = {
							checked: true,
							img: books[book_index].bookImg,
							bookName: books[book_index].bookName,
							author: books[book_index].author,
							price: books[book_index].price,
							discount: 2.0,
							booksElement: book_index,
							isbn:books[book_index].isbn,
							bookNum:1
						};
					
						buyCart.unshift(book);
					
						uni.setStorageSync("buyCart", buyCart);
					
						uni.navigateTo({
							url: '/pages/buyRentCart/buyRentCart?tabBar=0'
						});
				}else if(store.state.rentOffLine){
						store.state.rentOffLine = false;
						let book = {
							checked: true,
							img: books[book_index].bookImg,
							bookName: books[book_index].bookName,
							author: books[book_index].author,
							price: books[book_index].price,
							discount: 2.0,
							booksElement: book_index,
							isbn:books[book_index].isbn,
							bookNum:1
						};
					
						rentCart.unshift(book);
					
						uni.setStorageSync("rentCart", rentCart);

						uni.navigateTo({
							url: '/pages/buyRentCart/buyRentCart?tabBar=1'
						});
				}else{
					uni.navigateTo({
						url: '/pages/book_details/book_details?book_index=' + book_index
					});
				}
				
			} else {
				console.log(curPage);
				let book = {
					checked: true,
					img: books[book_index].bookImg,
					bookName: books[book_index].bookName,
					author: books[book_index].author,
					price: books[book_index].price,
					discount: 2.0,
					booksElement: book_index,
					isbn:books[book_index].isbn,
					bookNum:1
				};

				saleBooks.unshift(book);

				uni.setStorageSync("cart", saleBooks);

				if (curPage == 'sale') {
					uni.navigateTo({
						url: "/pages/cart/cart",

					});
				}
			}
		}).catch(reason => {

			uni.showModal({
				title: '错误',
				content: reason,
				mask: true,
				showCancel: false
			});

			console.log(reason);
		});



	}
}
